package com.chijun.mform.full.mode;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chijun.mform.create.mode.Form;
import com.chijun.mform.create.mode.FormApprovePerson;
import com.chijun.mform.create.mode.FormApproveProcess;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.create.mode.FormFieldOption;
import com.chijun.mform.create.mode.FormOptionReField;
import com.example.ccj.u.GenerateId;
import com.example.ccj.u.JsonUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ToastUtils;

import org.minidns.record.A;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务器表单配置mode
 */
public class ModeLoadServerFormConfigData {

    public long id;   //提交的表单该表单的指定id

    public Form form;   //表配置

    public List<FormField> formFieldList; //表字段配置
    private Map<Long,List<FormFieldOption>> formFieldOptionMap;//表选项配置 Map<字段id,List<选项mode>>
    public Map<Long,List<Long>> formOptionReFieldMap; //表选项关联字段配置  Map<选项id,List<关联字段id>>

    public List<FormApproveProcess> formApproveProcessList;   //表审批流程
    public Map<Long,List<FormApprovePerson>> formApprovePersonMap;  //表单审批人配置 Map<流程id,List<人员mode>>




    public Map<Long, List<FormFieldOption>> getFormFieldOptionMap() {
        return formFieldOptionMap;
    }
    public void setFormFieldOptionMap(Map<Long, List<FormFieldOption>> formFieldOptionMap) {
        this.formFieldOptionMap = formFieldOptionMap;
    }


    //根据服务器返回的json数据构造表单配置
    public static ModeLoadServerFormConfigData analysis(String formConfig){
        if (StringUtils.isEmpty(formConfig)) {
            ToastUtils.showShort("解析数据不可为空！");
            return null;
        }

        JSONObject jsonObject = JSONObject.parseObject(formConfig);
        ModeLoadServerFormConfigData writeFormData = new ModeLoadServerFormConfigData();

        if (jsonObject.containsKey("id")) {
            writeFormData.id = jsonObject.getLong("id");
            LogUtils.d(writeFormData.id);
        }

        //表单配置
        if (jsonObject.containsKey("form")) {
            String form = jsonObject.getString("form");
            if (!StringUtils.isEmpty(form)) {
                Form form1 = JSON.parseObject(form, Form.class);
                if (form1 != null) {
                    writeFormData.form = form1;
                } else {
                    ToastUtils.showShort("获取表单数据失败！解析完毕后为空！");
                    return null;
                }
            } else {
                ToastUtils.showShort("获取表单数据失败！返回的表单配置数据为空！");
                return null;
            }
        } else {
            ToastUtils.showShort("获取表单数据失败！返回的数据部包含表单配置数据！");
            return null;
        }

        //字段
        writeFormData.formFieldList = new ArrayList<>();
        if (jsonObject.containsKey("form_field")) {
            String fieldStr = jsonObject.getString("form_field");
            if (!StringUtils.isEmpty(fieldStr)) {
                List<FormField> formFields = JsonUtils.toListBean(fieldStr, FormField.class);

                if (formFields != null && !formFields.isEmpty()) {
                    writeFormData.formFieldList = formFields;
                    Collections.sort(writeFormData.formFieldList);
                } else {
                    ToastUtils.showShort("未获取到该表单字段配置！");
                    return null;
                }
            } else {
                ToastUtils.showShort("获取到该表单字段配置为空！");
                return null;
            }
        } else {
            ToastUtils.showShort("服务器未返回字段配置数据！");
            return null;
        }

        //选项
        writeFormData.formFieldOptionMap = new HashMap<>();
        if (jsonObject.containsKey("form_field_option")) {
            String formFieldOption = jsonObject.getString("form_field_option");
            if (!StringUtils.isEmpty(formFieldOption)) {
                List<FormFieldOption> formFieldOptionList = JsonUtils.toListBean(formFieldOption, FormFieldOption.class);
                if (formFieldOptionList != null && !formFieldOptionList.isEmpty()) {
                    writeFormData.formFieldOptionMap = new HashMap<>();
                    for (FormFieldOption fieldOption : formFieldOptionList) {
                        if (writeFormData.formFieldOptionMap.containsKey(fieldOption.fieldId)) {
                            List<FormFieldOption> formFieldOptions = writeFormData.formFieldOptionMap.get(fieldOption.fieldId);
                            if (formFieldOptions != null) formFieldOptions.add(fieldOption);
                        } else {
                            List<FormFieldOption> formFieldOptions = new ArrayList<>();
                            formFieldOptions.add(fieldOption);
                            writeFormData.formFieldOptionMap.put(fieldOption.fieldId,formFieldOptions);
                        }
                    }
                }
            }
        }

        //选项关联字段
        writeFormData.formOptionReFieldMap = new HashMap<>();
        if (jsonObject.containsKey("form_field_option_re")) {
            String formFieldOptionRe = jsonObject.getString("form_field_option_re");
            if (!StringUtils.isEmpty(formFieldOptionRe)) {
                List<FormOptionReField> formOptionReFieldList = JsonUtils.toListBean(formFieldOptionRe, FormOptionReField.class);
                if (formOptionReFieldList != null && !formOptionReFieldList.isEmpty()) {
                    writeFormData.formOptionReFieldMap = new HashMap<>();
                    for (FormOptionReField formOptionReField : formOptionReFieldList) {
                        if (writeFormData.formOptionReFieldMap.containsKey(formOptionReField.fieldOptionId)) {
                            List<Long> formOptionReFields = writeFormData.formOptionReFieldMap.get(formOptionReField.fieldOptionId);
                            if (formOptionReFields != null) formOptionReFields.add(formOptionReField.fieldId);
                        } else {
                            List<Long> formOptionReFields = new ArrayList<>();
                            formOptionReFields.add(formOptionReField.fieldId);
                            writeFormData.formOptionReFieldMap.put(formOptionReField.fieldOptionId,formOptionReFields);
                        }
                    }
                }
            }
        }


            //流程
            writeFormData.formApproveProcessList = new ArrayList<>();
            if (jsonObject.containsKey("form_approve_process")) {
                String formApproveProcessStr = jsonObject.getString("form_approve_process");
                if (!StringUtils.isEmpty(formApproveProcessStr)) {
                    List<FormApproveProcess> formApproveProcesses = JsonUtils.toListBean(formApproveProcessStr, FormApproveProcess.class);
                    if (formApproveProcesses != null && !formApproveProcesses.isEmpty()) {
                        writeFormData.formApproveProcessList = formApproveProcesses;
                    } else {
                        ToastUtils.showShort("流程表单未获取到该表单流程配置！");
                        return null;
                    }
                } else {
                    ToastUtils.showShort("流程表单获取到该表单流程配置为空！");
                    return null;
                }
            } else {
                ToastUtils.showShort("流程表单服务器未返回流程配置数据！");
                return null;
            }
            Collections.sort(writeFormData.formApproveProcessList); //排序

            //流程审批人
            writeFormData.formApprovePersonMap = new HashMap<>();
            if (jsonObject.containsKey("form_approve_person")) {
                String formApprovePerson = jsonObject.getString("form_approve_person");
                if (!StringUtils.isEmpty(formApprovePerson)) {
                    List<FormApprovePerson> formApprovePeople = JsonUtils.toListBean(formApprovePerson, FormApprovePerson.class);
                    if (formApprovePeople != null && !formApprovePeople.isEmpty()) {
                        writeFormData.formApprovePersonMap = new HashMap<>();
                        for (FormApprovePerson formPerson : formApprovePeople) {
                            if (writeFormData.formApprovePersonMap.containsKey(formPerson.processId)) {
                                List<FormApprovePerson> formApprovePersonList = writeFormData.formApprovePersonMap.get(formPerson.processId);
                                if (formApprovePersonList != null) formApprovePersonList.add(formPerson);
                            } else {
                                List<FormApprovePerson> formOptionReFields = new ArrayList<>();
                                formOptionReFields.add(formPerson);
                                writeFormData.formApprovePersonMap.put(formPerson.processId,formOptionReFields);
                            }
                        }
                    }
                }
            }



        return writeFormData;
    }

}
